<?php 
/*
 * 用来产生 PDR API 的 wiki 文档页面
 */

$arrFuncDeclareFiles = array(

	'Windows系统函数库' => '../../declare.api/funcs.system.php' ,
	'Windows窗口函数库' => '../../declare.api/funcs.window.php' ,
	'DHTML窗口函数库' => '../../declare.api/funcs.dhtml.php' ,
	'Windows托盘函数库' => '../../declare.api/funcs.tray_icon.php' ,
	'Windows菜单函数库' => '../../declare.api/funcs.menu.php' ,
	'文件、串口操作函数库' => '../../declare.api/funcs.file.php' ,
	'进程、管道函数库' => '../../declare.api/funcs.process.php' ,
	'Windows注册表函数库' => '../../declare.api/funcs.reg.php' ,
	'Win32资源函数库' => '../../declare.api/funcs.resource.php' ,

) ;


foreach ($arrFuncDeclareFiles as $sLibName=>$sDeclareFile)
{
	$arrOriFuncs = get_defined_functions() ;
	include $sDeclareFile ;
	$arrLibFuncs = get_defined_functions() ;

	MakeFuncsLib( array_diff($arrLibFuncs['user'],$arrOriFuncs['user']), $sLibName ) ;	
}


function MakeFuncsLib(array $arrFuncs,$sLibName)
{
	$sPageStream = "<h3>PDR-文档-{$sLibName}</h3> 返回[[PDR-文档-目录]]" ;
	
	foreach ($arrFuncs as $sFuncName)
	{
		$aFuncRef = new ReflectionFunction($sFuncName) ;
		$arrParamRefs = $aFuncRef->getParameters() ;
		
		// 参数表
		// -------------------------------------
		$arrParamListStream = array() ;		
		foreach ($arrParamRefs as $aParamRef)
		{
			// 参数 class
			if( $sClassName=$aParamRef->getClass() )
			{
				$sParamStream.= $sClassName.' ' ;
			}
			
			// 引用传递
			if( $aParamRef->isPassedByReference() )
			{
				$sParamStream.= '&' ;
			}
			
			// 参数名称
			$sParamStream = '$'. $aParamRef->getName() ;
			
			// 缺省值
			if( $aParamRef->isDefaultValueAvailable() )
			{
				$sValue = var_export($aParamRef->getDefaultValue(),1) ;
				$sValue = str_replace("\r",'',$sValue) ;
				$sValue = str_replace("\n",'',$sValue) ;
				$sParamStream.= '='.$sValue ;
			}
			
			$arrParamListStream[] = $sParamStream ;
		}
		
		$sParamListStream = ' '.implode(', ',$arrParamListStream).' ' ;
		$sParamListStream = trim($sParamListStream) ;
		
		// 函数注释
		// -------------------------------------
		$sFuncDoc = $aFuncRef->getDocComment() ;
		$sDocComment = TrimDocComment($sFuncDoc) ;
		
		// 返回值
		$sReturn = TrimDocComment($sFuncDoc,'return') ;
		if($sReturn)
		{
			$sReturn = "<b>return</b> {$sReturn}" ;
		}
		
		
		$sPageStream.= "
<hr />
<span class=\"function_name\">{$sFuncName}</span>({$sParamListStream});<br />
{$sDocComment}<br />
{$sReturn}
" ;
	}
	
	
	$sOutfile = iconv('utf-8','gb2312',"pdr_api_pages/{$sLibName}.html") ;
	file_put_contents($sOutfile,$sPageStream) ;
}

function ReflectionFunction($sFuncName)
{
}

function TrimDocComment($sDocComment,$sFld=null)
{
	$arrLines = explode("\n",$sDocComment) ;
	foreach ($arrLines as &$sLine)
	{
		$sLine = preg_replace("|^\\s*/\\*\\*|",'',$sLine);
		$sLine = preg_replace("|^\\s* \\* |",'',$sLine);
		$sLine = preg_replace("|^\\s* \\*/|",'',$sLine);
		$sLine = str_replace(' ','&nbsp;',$sLine) ;
		$sLine = str_replace("\t",'　　',$sLine) ;

		$arrResult = array() ;
		if( preg_match('/^\s*@(([^\s]+)(.*)$)?/',$sLine,$arrResult) )
		{
			if( strtolower($arrResult[2])==strtolower($sFld) )
			{
				return trim($arrResult[3]) ;
			}
			$sLine = '' ;
		}
	}
	
	if( $sFld )
	{
		return null ;
	}
	
	else 
	{
		$sComment = implode("\n",$arrLines) ;
		$sComment = trim($sComment) ;
		$sComment = nl2br($sComment) ;
		
		return $sComment ;
	}
}
?>